package cp3120.view;

import generic.swing.List2AbstractTableAdapater;
import cp3120.model.Flight;
import cp3120.ucm.client.UCDisplayFlight;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.*;

/**
 * A Panel that displays a FlightTableUI
 * @see FlightTableUI
 * @author Adam Scarr
 * @since r1
 */
public class FlightList extends JScrollPane {
    private JTable table;
    private List2AbstractTableAdapater<Flight> data;

    /**
     * Class Constructor with component initialization.
     */
    public FlightList(List<Flight> flights) {
        data = new List2AbstractTableAdapater<Flight>(flights, new String[] {"getPrice", "getFlightID"});
        data.replaceHeading("Ticket", "Ticket Price");

        table = new JTable(data);

        setViewportView(table);
        setPreferredSize(new Dimension(800, 300));

        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    rowClicked(data.getRow(table.getSelectedRow()));
                }
            }
        });
    }

    public void rowClicked(Flight flight) {
        new UCDisplayFlight(flight).run();
    }

    void update(List<Flight> flights) {
        data = new List2AbstractTableAdapater<Flight>(flights);
        table.setModel(data);
        repaint();
    }
}
